import os

from dotenv import load_dotenv, set_key


class LoadEnvUtil:

    @classmethod
    def get_env_params(cls, key, default=None):
        """
        :param key: env variable {Key}
        :param default: Loading the env variable, Get {Key-->Value}
        :return:
        """
        load_dotenv()
        return os.environ.get(key, default)

    @classmethod
    def set_env_params(cls, env_file, params, value):
        """
        :param params: env variable {Key}
        :param value: env variable {Value}
        :return: Check whether the env file exists, and create it if it does not
        """
        if not os.path.exists(env_file):
            with open(env_file, 'w') as f:
                pass  # Create an empty.env file

        load_dotenv()
        set_key(env_file, params, value, quote_mode='never')
        print(f"{params} has been written to the env file")

    @classmethod
    def parse_notifier_officer(cls, key, default="@all"):
        """
        Parse a given env variable into a list
        :param key: The key of the environment variable
        :param default: The default value if the key is not found
        :return: List of officers, default ["@all"]
        """
        notifier_officer = cls.get_env_params(key, default)
        if not notifier_officer:
            return [default]
        officers = [officer.strip() for officer in notifier_officer.split(",") if officer.strip()]
        return officers if officers else [default]

